/**CocosApi*/
export class Cocos_Api_MG {//萌果专用
  constructor() {
  }
  /**
   * 角度转弧度
   * @param {*} angle 角度
   */
  static degreesToRadians(angle) {
      return angle * Cocos_Api_MG.RAD;
  };
  /**
   * 向量旋转
   * @param {*} vecotr2 向量{x:0,y:0} 
   * @param {*} radians 弧度
   * @returns {{x:Number,y:Number}}
   */
  static rotateSelf_v2(vecotr2 = { x: 0, y: 0 }, radians = 0) {
      var sin = Math.sin(radians);
      var cos = Math.cos(radians);
      var x = vecotr2.x;
      vecotr2.x = cos * x - sin * vecotr2.y;
      vecotr2.y = sin * x + cos * vecotr2.y;
      return vecotr2;
  }
  /**
   * 获取目标物体对应当前物体的角度值
   * @param {*} start {x:当前物体x,y：当前物体y}
   * @param {*} end  {x:目标物体x，y:目标物体y}
   */
  static GetRotationByTwoNode(start, end) {

      var diff_x = end.x - start.x;
      var diff_y = end.y - start.y;
      if (diff_x == 0 && diff_y == 0) {
          return 0;
      }
      var jd = 360 * Math.atan(diff_y / diff_x) / (2 * Math.PI);

      if (diff_x >= 0 && diff_y <= 0) {
          return jd + 360;
      } else if (diff_x >= 0 && diff_y >= 0) {
          return jd
      } else if (diff_x <= 0 && diff_y >= 0) {
          return jd + 180
      } else if (diff_x <= 0 && diff_y <= 0) {
          return jd + 180
      }

  }
  /**
   * 获取目标物体对应当前物体的距离
   * @param {*} start {x:当前物体x,y：当前物体y}
   * @param {*} end  {x:目标物体x，y:目标物体y}
   */
  static GetDistanceByTwoNode(start, end) {
      return Math.sqrt((start.x - end.x) * (start.x - end.x) + (start.y - end.y) * (start.y - end.y));
  }
  /**
   * 判断三点是否共线 /判断q点是否在P1 P2之间 尽量整数
   * @param {*} Pi {x:0,y:0} 
   * @param {*} Pj {x:0,y:0} 
   * @param {*} Q {x:0,y:0} 
   */
  static onSegment(Pi, Pj, Q) {
      if ((Q.x == Pi.x && Q.y == Pi.y) || (Q.x == Pj.x && Q.y == Pj.y) || (Pj.x == Pi.x && Pj.y == Pi.y)) {
          return false
      }
      if ((Q.x - Pi.x) * (Pj.y - Pi.y) == (Pj.x - Pi.x) * (Q.y - Pi.y)  //叉乘 
          //保证Q点坐标在pi,pj之间 
          && Cocos_Api_MG.min(Pi.x, Pj.x) <= Q.x && Q.x <= Cocos_Api_MG.max(Pi.x, Pj.x)
          && Cocos_Api_MG.min(Pi.y, Pj.y) <= Q.y && Q.y <= Cocos_Api_MG.max(Pi.y, Pj.y))
          return true;
      else
          return false;
  }
  /**找出两个数中的最小值 */
  static min(value1, value2) {
      if (value1 > value2) {
          return value2
      } else {
          return value1
      }
  }
  /**找出两个数中的最大值 */
  static max(value1, value2) {
      if (value1 > value2) {
          return value1
      } else {
          return value2
      }
  }
  /**数组hash去重 */
  static uniArray(arr) {
      var result = [], hash = {};
      for (var i = 0, elem; (elem = arr[i]) != null; i++) {
          if (!hash[elem]) {
              result.push(elem);
              hash[elem] = true;
          } else {
              console.log(elem);
          }
      }
      return result;
  }
  /**
* 获得旋转夹角
* @param {*} x1 旋转点1
* @param {*} y1
* @param {*} x2 旋转点2
* @param {*} y2
*/
  static getAngle(x1, y1, x2, y2) {


      //2个点之间的角度获取
      let c1 = Math.atan2(y1, x1) * 180 / (Math.PI);
      let c2 = Math.atan2(y2, x2) * 180 / (Math.PI);
      let angle;
      c1 = c1 <= -90 ? (360 + c1) : c1;
      c2 = c2 <= -90 ? (360 + c2) : c2;
      //夹角获取
      angle = Math.floor(c2 - c1);
      angle = angle < 0 ? angle + 360 : angle;
      return angle;
  }
  /**
   * 秒转换为00:00格式
   * @param {*} m 
   */
  static MtoAABB(m = 0) {
      var Fen = Math.floor(m / 60);
      var miao = m % 60;
      if (Fen < 10) {
          Fen = "0" + Fen;
      }
      if (miao < 10) {
          miao = "0" + miao;
      }
      return Fen + ":" + miao
  }
  /**
   * 数据转换 B==>KB==>MB==>GB....
   * @param {*} a  字节长度
   * @param {*} b 保留多少位小数
   */
  static formatBytes(a, b) {
      if (0 == a) return "0 B";
      var c = 1024, d = b || 2, e = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"], f = Math.floor(Math.log(a) / Math.log(c));
      return parseFloat((a / Math.pow(c, f)).toFixed(d)) + " " + e[f];
  }
  /**
      * 贝塞尔 抛物线
      * @param p0 起点
      * @param p2 终点
      * @param height 高度
      * @param t 进度0-1
      */
  static bezier(p3 = new Laya.Vector2(), p2_2 = new Laya.Vector2(), height = 1, t = 0) {
      var p0 = new Laya.Vector2(p3.x, p3.y);
      var p2 = new Laya.Vector2(p2_2.x, p2_2.y);
      let t1 = (1 - t) * (1 - t);
      let t2 = 2 * t * (1 - t);
      let t3 = t * t;
      let p1 = { x: p0.x + (p2.x - p0.x) / 2, y: p0.y + height };
      p0 = Cocos_Api_MG.ber_multiplyScalar(p0, t1);
      p1 = Cocos_Api_MG.ber_multiplyScalar(p1, t2);
      p0 = Cocos_Api_MG.ber_add(p0, p1);
      p2 = Cocos_Api_MG.ber_multiplyScalar(p2, t3);
      p0 = Cocos_Api_MG.ber_add(p0, p2);
      // return p0.multiplyScalar(t1).add(p1.multiplyScalar(t2)).add(p2.multiplyScalar(t3));
      return p0;
  }
  /**
   * @en Vector scalar multiplication and save the results to out vector object
   * @zh 向量标量乘法
   */
  static ber_multiplyScalar(out, scalar) {
      out.x *= scalar;
      out.y *= scalar;
      return out;
  }

  /**
     * @en Element-wise vector addition and save the results to out vector object
     * @zh 逐元素向量加法
     */
  static ber_add(out, other) {
      out.x += other.x;
      out.y += other.y;
      return out;
  }

  /**获取字符串 长度/KB */
  static strSize(str, charset = "uft8") {
      let total = 0;
      for (var i = 0; i < str.length; i++) {
          let charCode = str.charCodeAt(i);
          if (charset === 'utf-16' || charset === 'utf16') {
              total += charCode <= 0xffff ? 2 : 4;
          } else {
              if (charCode <= 0x007f) {
                  total += 1;
              } else if (charCode <= 0x07ff) {
                  total += 2;
              } else if (charCode <= 0xffff) {
                  total += 3;
              } else {
                  total += 4;
              }
          }
      }
      return total;
  }
  /**数字转16进制双字符串 */
  static decimalToHexString(number) {
      if (number < 0) {
          number = 0xFFFF + number + 1;
      }
      return number.toString(16).toUpperCase();
  }
  /**16进制双字节字符串转数字 */
  static HexStringTodecimal(Str) {
      var int = 0xFFFF;
      var To = parseInt(Str, 16);
      if (To > 32767) {
          To = To - int - 1;
      }
      return To;
  }
  /**16进制双字节字符串转数字 */
  static HexStringTodecimal2(win = '') {
      win = parseInt(win, 16);
      var i_rc = 0;
      var i_sign = 1;
      i_sign = ((win & (1 << 15)) > 0) ? -1 : 1;
      if (i_sign < 0) {
          win -= 1;
          i_rc = (-win & 0xFFFF);
      } else {
          i_rc = win;
      }
      return i_rc
  }
  /**
   * 数字转16进制字符串 只保留num位 
   * @param {*} number 
   * @param {*} number 
   */
  static HexNumberToString(number, num) {
      return ('0'.repeat(num) + number.toString(16)).slice(-num);
  }
  /**
* 数字转16进制字符串 只保留num位 倒叙显示 256=>0100<=0010  
* @param {*} number 
* @param {*} num  只支持 2  4 8 位
*/
  static HexNumberToString2(number, num) {
      var txt = Cocos_Api_MG.HexNumberToString(number, num);
      var arr = txt.split("");
      if (num == 2) {
          return (arr[1] + arr[0]);
      } else if (num == 4) {
          return (arr[2] + arr[3] + arr[0] + arr[1]);
      } else if (num == 8) {
          return (arr[6] + arr[7] + arr[4] + arr[5] + arr[2] + arr[3] + arr[0] + arr[1]);
      }
  }
  /**
   * Uint8Array转字符串
   * @param {Uint8Array} fileData  数据
   */
  static UT8ArrayToStr(fileData) {
      var dataString = "";
      for (var i = 0; i < fileData.length; i++) {
          dataString += String.fromCharCode(fileData[i]);
      }
      return dataString
  }
  /**
   * arraybuffer类型转16进制字符串
   * @param {ArrayBuffer} buffer 
   * @returns {String}
   */
  static ArrayBufferToStr(buffer) {
      return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join(' ');
  }
  /**
 * arraybuffer类型转16进制字符串 中间无空格
 * @param {ArrayBuffer} buffer 
 * @returns {String}
 */
  static ArrayBufferToStr2(buffer) {
      return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
  }
  /**16进制字符串 转arraybuffer类型*/
  static StrToArrayBuffer(msg) {
      var typedArray = new Uint8Array(msg.match(/[\da-f]{2}/gi).map(function (h) {
          return parseInt(h, 16);
      }))
      return typedArray;
  }
  /**16进制arraybuffer转字符串 */
  static ArrayBufferToStr10(buffer) {
      var str = Cocos_Api_MG.ArrayBufferToStr2(buffer);
      return Cocos_Api_MG.Byte16toStr(str)
  }
  /**16进制转字符串 */
  static Byte16toStr(str) {
      var val = "";
      var arr = str.split(" ");
      for (var i = 0; i < arr.length; i++) {
          val += String.fromCharCode(parseInt(arr[i], 10));
      }
      return val;
  }
  /**字符串转16进制 */
  static StrToByte16(str) {
      var val = "";
      for (var i = 0; i < str.length; i++) {
          val += str.charCodeAt(i).toString(16);

      }
      return val
  }

  /**
  * 字符串转Uint8Array
  * @param {string} fileData  字符串
  */
  static UT8ArrayFromStr(str) {
      var arr = [];
      for (var i = 0, j = str.length; i < j; ++i) {
          arr.push(str.charCodeAt(i));
      }

      var tmpUint8Array = new Uint8Array(arr);
      return tmpUint8Array
  }
  /**获取随机十六进制颜色 */
  static getRandomColor() {
      var color = "#";
      for (var i = 0; i < 6; i++) color += parseInt(Math.random() * 16).toString(16);
      return color;

  }
  /**
   * !#zh 逐元素向量减法
   * @method subtract
   * @typescript
   * subtract <Out extends IVec2Like> (out: Out, a: Out, b: Out): Out
   */
  static subtract(point1, point2) {///cocos版本
      var x = point1.x - point2.x;
      var y = point1.y - point2.y;
      return { x: x, y: y };
  }
  /**
     * !#zh 求向量长度平方
     * @method lengthSqr
     * @typescript
     * lengthSqr <Out extends IVec2Like> (a: Out): number
     */
  static lengthSqr(a) {///cocos版本
      var _x = a.x;
      var _y = a.y;
      return _x * _x + _y * _y;
  }
  /**
* !#zh 向量点积（数量积）
* @method dot
* @typescript
* dot <Out extends IVec2Like> (a: Out, b: Out): number

*/
  static dot(point1, point2) {///cocos版本
      return point1.x * point2.x + point1.y * point2.y;
  }
  /**
   * 二维向量 归一化
   * @param {*} s {x:0,y:0}
   */
  static normalize(s) {
      var out = { x: 0, y: 0 };
      var x = s.x, y = s.y;
      var len = x * x + y * y;
      if (len > 0) {
          len = 1 / Math.sqrt(len);
          out.x = x * len;
          out.y = y * len;
      }
      return out;
  }
  /**
   * 矩形碰撞检测 是否碰撞
   * @param {*} objA 对象A 包含(x,y,width,height)
   * @param {*} objB 对象B 包含(x,y,width,height)
   * @returns {CollisionRT} TF是否碰撞 BFB交集面积百分比(A有多少部分相交于B 保留两位小数);
   */
  static Collision(objA, objB) {
      var L1 = objA.x;
      var T1 = objA.y;
      var R1 = objA.x + objA.width;
      var B1 = objA.y + objA.height;

      var L2 = objB.x;
      var T2 = objB.y;
      var R2 = objB.x + objB.width;
      var B2 = objB.y + objB.height;
      var TTFF = !(L1 > R2 || T1 > B2 || R1 < L2 || B1 < T2);
      var BFB = 0;
      if (TTFF) {
          var width = (Math.min(R1, R2) - Math.max(L1, L2));
          var height = (Math.min(B1, B2) - Math.max(T1, T2));
          var S = width * height;
          var AllS = objA.width * objA.height;
          BFB = Math.floor(S / AllS * 100) / 100;
      }
      return new CollisionRT(TTFF, BFB)

  }




  static loUint16(v) {
      return (v & 0xFF);
  }

  static hiUint16(v) {
      return (v >> 8);
  }

  static buildUint16(hi, lo) {
      if ((hi & 0x80) == 0) {
          return (((hi) << 8) | (lo & 0xff));
      } else {
          var temphi = hi & 0x7f;

          return ((temphi << 8) | (lo & 0xff) | 0x8000);
      }
  }

  static Uint32_Lo1(v) {
      return (v & 0xff);
  }

  static Uint32_Lo2(v) {
      return ((v >> 8) & 0xff);
  }

  static Uint32_Lo3(v) {
      return ((v >> 16) & 0xff);
  }

  static Uint32_Lo4(v) {
      return (v >> 24);
  }

  static buildUint32(byte_1, byte_2, byte_3, byte_4) {
      if ((byte_1 & 0x80) == 0) {
          return ((((byte_1 & 0xff)) << 24) | (((byte_2 & 0xff)) << 16) | (((byte_3 & 0xff)) << 8) | (byte_4 & 0xff));
      } else {
          var temphi = byte_1 & 0x7f;

          return ((temphi << 24) | (((byte_2 & 0xff)) << 16) | (((byte_3 & 0xff)) << 8) | (byte_4 & 0xff) | 0x80000000);
      }
  }

   /**获取圆形随机点 */
   static get_random_pos(center_x, center_y, radius) {
      var RAND = Math.random();
      var u = Math.sqrt(RAND) * radius;
      var v = RAND * 2 * Math.PI;
      var x = center_x + u * Math.cos(v);
      var y = center_y + u * Math.sin(v);
      return { x: x, y: y }
  }
}
Cocos_Api_MG.RAD = Math.PI / 180;
Cocos_Api_MG.DEG = 180 / Math.PI;
class CollisionRT {
  constructor(TF = false, BFB = 0) {
      this.TF = TF;
      this.BFB = BFB;
  }
}
